//
//  MyOrderView.m
//  XRmenu-HD
//
//  Created by shengchao yang on 11-12-14.
//  Copyright (c) 2011年 home user. All rights reserved.
//

#import "MyOrderView.h"
#import "MyOrderViewCell.h"

@implementation MyOrderView
float price;

-(id) initWithStyle:(NSString*)stylename DishSubViewName:(NSString*)vname{
    self = [super initWithStyle:stylename DishSubViewName:vname];
    price=0.0; 
    if (self) {   
        //画出tableview
        NSDictionary *tdict = [[[[APPSESSION.allconfig objectForKey:@"UI Skin"] objectForKey:stylename] objectForKey:@"Main View"] objectForKey:@"MyOrder View"];
        NSDictionary *tabdict = [[[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"TableView CSS"] objectForKey:[tdict getStringValueAtPath:@"sub tableview/table view style"]];
        if (tdict) {
            [self setTableViewProperty:tabdict Frame:CGRectFromString([tdict getStringValueAtPath:@"sub tableview/table view rect"])];
        }
        UIColor *btn_color = RGBA(235.0, 217.0, 185.0, 254.0/ 255.0f);
        NSString *btn_bkg=[XRmenuPublic getGeneralSkinImg:@"MainView/MyOrderView/bt_myorder_main.png"];
        
        if ([self viewWithTag:kTagOfStartBT + 0]) {
            UIButton *btn =(UIButton *) [self viewWithTag:kTagOfStartBT + 0];
            [btn.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
            [btn setTitle:loc(@"送单") forState:UIControlStateNormal];
            [btn setTitleColor:btn_color forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageWithContentsOfFile:btn_bkg] forState:UIControlStateNormal];
        }
        if ([self viewWithTag:kTagOfStartBT + 1]) {
            UIButton *btn =(UIButton *) [self viewWithTag:kTagOfStartBT + 1];
            [btn.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
            [btn setTitle:loc(@"清空") forState:UIControlStateNormal];
            [btn setTitleColor:btn_color forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageWithContentsOfFile:btn_bkg] forState:UIControlStateNormal];
        }
        if ([self viewWithTag:kTagOfStartBT + 2]) {
            UIButton *btn =(UIButton *) [self viewWithTag:kTagOfStartBT + 2];
            [btn.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
            [btn setTitle:loc(@"返回") forState:UIControlStateNormal];
            [btn setTitleColor:btn_color forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageWithContentsOfFile:btn_bkg] forState:UIControlStateNormal];
        }
        //2.初始化预计消费
        price=0.0;
        if ([APPSESSION.myOrders count]>0) {
            for (DishOrder *oneOrder in APPSESSION.myOrders) {
                price=price + [oneOrder.ddPrice floatValue]*[oneOrder.ddNumber floatValue];
                if ([self viewWithTag:kTagOfStartLabel+6]) {
                    [(UILabel *)[self viewWithTag:kTagOfStartLabel+6] setText:[NSString stringWithFormat:@"%@ %@%0.2f",loc(@"预计消费"),[APPSESSION.allconfig getStringValueAtPath:@"System Config/currency"],price]];
                }
            }
        }
        else{
            [(UILabel *)[self viewWithTag:kTagOfStartLabel+6] setText:[NSString stringWithFormat:@"%@ %@%0.2f",loc(@"预计消费"),[APPSESSION.allconfig getStringValueAtPath:@"System Config/currency"],price]];
        }
        addNObserver(@selector(reloadTableViewData), kMessage_MyOrderView_ReloadTableViewData);
    }
    return self;
}
//被子view调用，更新tableview数据
-(void) updateMyOrderData{
    //1.界面上更新我的点单数量
    postN(kMessage_MainView_MyOrderNumberChanged);
    postN(kMessage_CommendTable_MyOrderNumberChanged);
    //2.刷新tableview
    postN(kMessage_MyOrderView_ReloadTableViewData);
}
-(void) reloadTableViewData{
    //1.初始化预计消费
    price=0.0;
    if ([APPSESSION.myOrders count]>0) {
        for (DishOrder *oneOrder in APPSESSION.myOrders) {
            price=price + [oneOrder.ddPrice floatValue]*[oneOrder.ddNumber floatValue];
            if ([self viewWithTag:kTagOfStartLabel+6]) {
                [(UILabel *)[self viewWithTag:kTagOfStartLabel+6] setText:[NSString stringWithFormat:@"%@ %@%0.2f",loc(@"预计消费"),[APPSESSION.allconfig getStringValueAtPath:@"System Config/currency"],price]];
            }
        }
    }
    else{
        [(UILabel *)[self viewWithTag:kTagOfStartLabel+6] setText:[NSString stringWithFormat:@"%@ %@%0.2f",loc(@"预计消费"),[APPSESSION.allconfig getStringValueAtPath:@"System Config/currency"],price]];
    }
    //2.刷新tableview
    [((UITableView *)[self viewWithTag:kTagOfStartTableView]) reloadData];
}

#pragma mark 重载button处理方法
-(void) buttonClicked:(id)sender
{
	NSInteger bid = [sender tag];
     if (bid == (kTagOfStartBT + 0)) {        
        DebugLog(@"点击了送单按钮");
         [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
         if ([APPSESSION.myOrders count]==0) {
             UIAlertView* alert =[[ UIAlertView alloc] initWithTitle:loc(@"提示") 
                                                             message:loc(@"发送点单为空")
                                                            delegate:self 
                                                   cancelButtonTitle:loc(@"确定") 
                                                   otherButtonTitles:nil];
             [alert show];
             [alert release];
         }
         else{
             postN(kMessage_RootView_SendOrderViewDisplay);
         }
	}
	else if (bid == (kTagOfStartBT + 1)) {        
        DebugLog(@"点击了清空按钮");
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        
        if ([APPSESSION.myOrders count]==0) {
            UIAlertView *alert =[[ UIAlertView alloc] initWithTitle:loc(@"提示") 
                                                            message:loc(@"删除点单为空")
                                                           delegate:self 
                                                  cancelButtonTitle:loc(@"确定") 
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
        else{
            UIAlertView *alert =[[ UIAlertView alloc] initWithTitle:loc(@"删除确认") 
                                                            message:loc(@"清空确定")
                                                           delegate:self 
                                                  cancelButtonTitle:loc(@"取消") 
                                                  otherButtonTitles:loc(@"确定"), nil];
            [alert show];
            [alert release];
        }
	}
    else if (bid == (kTagOfStartBT + 2)) {        
        DebugLog(@"点击了返回按钮");
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        postN(kMessage_RootView_MyOrderViewDisappear);
	}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==alertView.cancelButtonIndex) {
        DebugLog(@"点击了取消按钮");
        return;
    }
    else{
        DebugLog(@"点击了确定按钮");
        XRmenuError *outErr=nil;
        [XRmenuCoreData deleteAllDishOrder:&outErr];
        [self updateMyOrderData];//更新tableview里的数据
    }
}

#pragma mark -
#pragma mark Tableview delegate and datasource
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{    
    return [APPSESSION.myOrders count];
}
//自定义row
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier1 = @"PluPluCell1";
	MyOrderViewCell *cell = (MyOrderViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
	if (cell == nil){
		NSString *config = [APPSESSION.allconfig getStringValueAtPath:[NSString stringWithFormat:@"UI Skin/%@/Main View/MyOrder View/sub tableview/table cell style", APPSESSION.uistyle]];
		cell = (MyOrderViewCell *)[[[MyOrderViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1 Customer:config] autorelease];
	}
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    DishOrder *oneOrder=(DishOrder *)[APPSESSION.myOrders objectAtIndex:indexPath.row];
    cell.celldata = oneOrder;
    [cell setID:indexPath.row+1];
    cell.delegate=self;    
    return cell;
}
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    APPSESSION.currentPluPlu = ((DishOrder *)[APPSESSION.myOrders objectAtIndex:indexPath.row]).rsToPluPlu;
    postN(kMessage_RootView_PhotoPopoverViewDisplay);
}

//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc
{
    removeNObserver(kMessage_MyOrderView_ReloadTableViewData);
    [super dealloc];
} 
@end
